package com.lowang.springboot01;

import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.http.MediaType;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.web.WebAppConfiguration;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.MvcResult;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import org.springframework.web.context.WebApplicationContext;

/**
 * 
 * 使用伪造的http请求模式测试. Unit test for simple App.
 */
@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(classes = App.class)
@WebAppConfiguration
public class AppTest2 {
    MockMvc mvc;
    @Autowired
    WebApplicationContext wac;
    @Before
    public void setUp() {
        mvc = MockMvcBuilders.webAppContextSetup(wac).build();
    }
    @Test
    public void testHome() throws Exception {
        String name = "zhengmo";
        String uri = "/home";
        MvcResult result = mvc.perform(MockMvcRequestBuilders.get(uri).accept(MediaType.APPLICATION_JSON_UTF8).param("name", "zhengmo")).andReturn();
        int status = result.getResponse().getStatus();
        String json = result.getResponse().getContentAsString();
        System.out.println(json);
        Assert.assertTrue("返回结果", name.equals(json));
        Assert.assertTrue("返回状态", status == 200);
    }
}
